﻿Public Class frmReglerMAJ
    Public pCodeBanque As Integer
    Public pCodeClient As Integer

    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing

    Private Sub frmReglerMAJ_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Me.Text = "VERSEMENT No : " & pCodeBanque
        Call InitComboJournal(CB_JOURNAL)
        Call InitComboModeRegle(CB_MODE)
        VersementToScreen()

    End Sub

    Private Sub VersementToScreen()

        ' Nouvelle Création ?
        If pCodeBanque = 0 Then
            ' RAB des champs du form
            ClearTextBoxes(Me)
            TE_DATE.Value = Now
            TE_MONTANT.Value = 0
            Select Case gSocieteCode
                Case "PE"
                    CB_JOURNAL.SelectedIndex = CB_JOURNAL.FindString("BA")
                Case "PO"
                    CB_JOURNAL.SelectedIndex = CB_JOURNAL.FindString("GE")
                Case "PL"
                    CB_JOURNAL.SelectedIndex = CB_JOURNAL.FindString("BQ")
                Case Else
                    CB_JOURNAL.SelectedIndex = CB_JOURNAL.FindString("SG")
            End Select
        Else
            'Lire le versement
            szSQL = "CALL spGET_LIV_BNQ('byCode','" & gSocieteCode & "'," & pCodeBanque & ",0,'','')"
            GetRecordset(ctDSN_DEVFAC, szSQL, rs)
            If rs.RecordCount < 1 Then
                rs.Close()
                Exit Sub
            End If

            CB_MODE.SelectedIndex = CB_MODE.FindString(rs.Fields("modereg").Value)
            TE_DATE.Value = rs.Fields("date").Value
            TE_BANQUE.Text = nv(rs.Fields("ets").Value)
            TE_JST.Text = nv(rs.Fields("jst").Value)
            TE_MONTANT.Value = rs.Fields("montantregl").Value
            TE_VENTILE.Value = rs.Fields("montantdebit").Value
            CB_JOURNAL.SelectedIndex = CB_JOURNAL.FindString(rs.Fields("jnl").Value)
        End If
    End Sub

    Private Sub BO_ENREGISTER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTER.Click
        If TE_MONTANT.Value = 0 Then
            MsgBox("Veuillez saisir un montant", MsgBoxStyle.Critical)
            Exit Sub
        End If

        szSQL = "CALL spUPD_LIV_BNQ(" & pCodeBanque & ",'" & gSocieteCode & "'," & pCodeClient & "," & CB_MODE.SelectedIndex + 1 & ",'" & _
                 Format(TE_DATE.Value, "yyyy/MM/dd") & "'," & TE_MONTANT.Value & ",'" & TE_BANQUE.Text & "','" & TE_JST.Text & "','" & _
                 CB_JOURNAL.SelectedItem.ToString.Substring(0, 2) & "')"
        ExecQuery(ctDSN_DEVFAC, szSQL)
        Me.Close()

    End Sub

    Private Sub BO_SUPPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_SUPPRIMER.Click

        If TE_VENTILE.Value <> 0 Then
            MsgBox("Ce versement est déjà ventilé", MsgBoxStyle.Critical)
            Exit Sub
        End If
        ' utilisateur autorisé à supprimer ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        deleteEnregistrement("LIV_BNQ", "Le versement de " & TE_MONTANT.Value.ToString("C"), pCodeBanque)
        ' réaffiche la viewlist
        Me.Close()

    End Sub
End Class